package org.lora.biz.ou;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.lora.biz.BizCommonConstant;
import org.lora.biz.ou.entity.Employee;
import org.lora.biz.ou.entity.EmployeeIdentity;
import org.lora.biz.ou.entity.EmployeeIdentityView;
import org.lora.biz.ou.entity.Position;
import org.lora.biz.ou.exception.LoraOrgUserException;
import org.lora.biz.ou.service.IEmployeeIdentityService;
import org.lora.core.annotation.Action;
import org.lora.core.annotation.AutoAssemble;
import org.lora.core.mybatis.Page;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.webframework.common.CommonConstant;
import org.lora.webframework.common.JsonResult;
import org.lora.webframework.common.WebframeBaseAction;
import org.lora.webframework.util.MD5Util;

import com.alibaba.fastjson.JSON;

/**
 * <p>
 * 类名:EmployeeIdentityAction
 * </p>
 * 描述:人员身份职务管理Action<br>
 * 创建时间:2016年8月31日 上午10:08:39<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年8月31日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/biz/ou/employee")
public class EmployeeIdentityAction extends WebframeBaseAction {

	@AutoAssemble
	private IEmployeeIdentityService employeeIdentityService;

	/**
	 * 跳转到人员管理页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showEmployeeManage")
	public ViewData showEmployeeManage() {
		return new ViewData("/jsp/ou/employeeManage.jsp");
	}

	/**
	 * 跳转到编辑人员信息页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/showEmployeeEdit")
	public ViewData showEmployeeEdit(HttpServletRequest request) {

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put(LoraOUConstant.DOMAIN_ID_KEY, domainId);

		return new ViewData("/jsp/ou/employeeEdit.jsp", dataMap);
	}

	/**
	 * 跳转到职务管理页面
	 * 
	 * @return
	 */
	@RequestPath(path = "/showPositionManage")
	public ViewData showPositionManage() {
		return new ViewData("/jsp/ou/positionManage.jsp");
	}

	/**
	 * 获取人员列表数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getEmployeeListData")
	@ResponseJSON
	public Object getEmployeeListData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();

		// 封装页面参数
		String[] parameterNameArray = new String[] { LoraOUConstant.DOMAIN_ID_KEY, "sortName", "sortOrder", "employeeName", "serialNumber",
				"accountName", "cellphoneNumber", "state" };
		Map<String, Object> parameterMap = super.packageParameterMapFromRequest(request, parameterNameArray, true);

		// 设置分页Bean
		Page page = super.getPageBean(request);

		String domainId = getStringFromMap(LoraOUConstant.DOMAIN_ID_KEY, parameterMap);

		try {
			List<Employee> list = employeeIdentityService.getEmployeeList(domainId, parameterMap, page, null);
			int count = employeeIdentityService.getEmployeeCount(domainId, parameterMap);
			resultMap.put("tableData", list);
			resultMap.put("count", count);
		} catch (Exception e) {
			businessLogger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * 获取职务列表数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getPositionListData")
	@ResponseJSON
	public Object getPositionListData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();

		// 封装页面参数
		String[] parameterNameArray = new String[] { LoraOUConstant.DOMAIN_ID_KEY, "sortName", "sortOrder", "positionName",
				"positionLevel", "state" };
		Map<String, Object> parameterMap = super.packageParameterMapFromRequest(request, parameterNameArray, true);

		// 设置分页Bean
		Page page = super.getPageBean(request);

		String domainId = getStringFromMap(LoraOUConstant.DOMAIN_ID_KEY, parameterMap);

		try {
			List<Position> list = employeeIdentityService.getPositionList(domainId, parameterMap, page, null);
			int count = employeeIdentityService.getPositionCount(domainId, parameterMap);
			resultMap.put("tableData", list);
			resultMap.put("count", count);
		} catch (Exception e) {
			businessLogger.error(e.getMessage(), e);
		}
		return resultMap;
	}

	/**
	 * 保存员工信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/saveEmployee")
	@ResponseJSON
	public JsonResult saveEmployee(HttpServletRequest request) {
		String jsonData = request.getParameter("jsonData");
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		JsonResult result = new JsonResult();

		result.setFlag(CommonConstant.FAIL);

		try {
			// 封装数据
			Employee employee = JSON.parseObject(jsonData, Employee.class);

			String password = employee.getAccountPassword();
			employee.setAccountPassword(MD5Util.encrypt(password).toUpperCase());

			// 验证数据
			if (validateEmployee(employee, domainId).equals(BizCommonConstant.VALIDATE_SUCCESS_STR)) {
				employeeIdentityService.saveEmployee(employee);
			}

			result.setFlag(CommonConstant.SUCCESS);

		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	private String validateEmployee(Employee employee, String domainId) throws LoraOrgUserException {
		// TODO 目前不验证
		return BizCommonConstant.VALIDATE_SUCCESS_STR;

	}

	/**
	 * 获取所有职务list
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/getAllPositionList")
	@ResponseJSON
	public JsonResult getAllPositionList(HttpServletRequest request) {

		JsonResult result = new JsonResult();
		result.setFlag(CommonConstant.FAIL);

		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		try {
			List<Position> positionList = employeeIdentityService.getPositionList(domainId, new HashMap<String, Object>(), null, null);
			result.setFlag(CommonConstant.SUCCESS);
			result.setData(positionList);
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;

	}

	/**
	 * 保存员工身份信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestPath(path = "/saveEmployeeIdentity")
	@ResponseJSON
	public JsonResult saveEmployeeIdentity(HttpServletRequest request) {
		String jsonData = request.getParameter("jsonData");
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);

		JsonResult result = new JsonResult();

		result.setFlag(CommonConstant.FAIL);

		try {
			// 封装数据
			EmployeeIdentity employeeIdentity = JSON.parseObject(jsonData, EmployeeIdentity.class);

			// 验证数据
			String msg = validateEmployeeIdentity(employeeIdentity, domainId);
			if (msg.equals(BizCommonConstant.VALIDATE_SUCCESS_STR)) {
				employeeIdentityService.saveEmployeeIdentity(employeeIdentity);
				result.setFlag(CommonConstant.SUCCESS);
			} else {
				result.setMsg(msg);
			}

		} catch (Exception e) {
			super.businessLogger.error(e.getMessage(), e);
		}

		return result;
	}

	private String validateEmployeeIdentity(EmployeeIdentity employeeIdentity, String domainId) {
		// TODO 目前不验证
		return BizCommonConstant.VALIDATE_SUCCESS_STR;
	}
	
	/**
	 * 获取身份列表数据(For 自动补全)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getEmployeeIdentityForComplete")
	@ResponseJSON
	public Object getEmployeeIdentityForComplete(HttpServletRequest request) {
		String keyword = request.getParameter("keyword");
		String domainId = request.getParameter(LoraOUConstant.DOMAIN_ID_KEY);
		Page page = new Page();
		page.setPageSize(10);
		page.setPageNumber(1);
		
		List<EmployeeIdentityView> list = null;
		try {
			list = employeeIdentityService.getEmployeeIdentityDetailForComplete(domainId, page, keyword, null);
		}catch(Exception e){
			businessLogger.error(e.getMessage(), e);
		}
		
		return list;
	}
	
	/**
	 * 获取身份列表数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getEmployeeIdentityListData")
	@ResponseJSON
	public Object getEmployeeIdentityListData(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();

		// 封装页面参数
		String[] parameterNameArray = new String[] { LoraOUConstant.DOMAIN_ID_KEY, "sortName", "sortOrder", "orgId" };
		Map<String, Object> parameterMap = super.packageParameterMapFromRequest(request, parameterNameArray, true);

		// 设置分页Bean
		Page page = super.getPageBean(request);

		String domainId = getStringFromMap(LoraOUConstant.DOMAIN_ID_KEY, parameterMap);

		try {
			List<EmployeeIdentityView> list = employeeIdentityService.getEmployeeIdentityDetailList(domainId, parameterMap, page, null);
			int count = employeeIdentityService.getEmployeeIdentityDetailCount(domainId, parameterMap);
			resultMap.put("tableData", list);
			resultMap.put("count", count);
		} catch (Exception e) {
			businessLogger.error(e.getMessage(), e);
		}
		return resultMap;
	}

}
